// 厦门大学计算机专业 | 前华为工程师
// 专注《零基础学编程系列》https://cxyxy.blog.csdn.net/article/details/121134634
// 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
// 公众号：花生皮编程

import SwiftUI
import AVFoundation
struct ContentView: View {
    @State var btnSize = CGFloat(130)
    @State var lightOn = false
    var body: some View {
        Button(action: {
            let cam = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
            
            try? cam?.lockForConfiguration()
            cam?.torchMode = self.lightOn ? .on : .off
            cam?.unlockForConfiguration()
            self.lightOn.toggle()
        }){
            Text(self.lightOn ? "关闭" : "打开")
                .frame(width: btnSize, height: btnSize)
                .foregroundColor(Color.white)
        }
        .background(Color.black)
        .cornerRadius(self.btnSize / 2)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
